﻿package com.sxhalo.manage.web.action;

import java.util.*;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.Company;
import com.sxhalo.manage.map.Companytype;
import com.sxhalo.manage.map.Projects;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.CompanyForm;

public class CompanyAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private CompanyForm company;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;

	private List dataList;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public CompanyForm getCompany() {
		return company;
	}

	public void setCompany(CompanyForm company) {
		this.company = company;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public IBaseService<Company> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<Company> myService) {
		this.myService = myService;
	}

	// 业务层
	private IBaseService<Company> myService;

	private IBaseService<Companytype> tmyService;

	private IBaseService<Projects> proService;

	public IBaseService<Companytype> getTmyService() {
		return tmyService;
	}

	public void setTmyService(IBaseService<Companytype> tmyService) {
		this.tmyService = tmyService;
	}

	public IBaseService<Projects> getProService() {
		return proService;
	}

	public void setProService(IBaseService<Projects> proService) {
		this.proService = proService;
	}

	/**
	 * 构造方法
	 */
	public CompanyAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

	public String getEntity() {
		try {
			Company entity = myService.findById(company.getCompanyId());
			BeanUtils.copyProperties(entity, company);
			company.setCompanyTypeId(entity.getCompanytype().getCompanyTypeId());
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			updateProjectNum();// 更新项目数
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;

			String whereStr = "";

			String companyTypeId = ServletActionContext.getRequest()
					.getParameter("typeid");

			if (!companyTypeId.equals("")) {
				whereStr += " companyTypeId = " + companyTypeId;
			}
			Pager<Company> list = myService.findPagerList(page, size, whereStr,
					"");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String getAllData() {
		// 获取数据
		try {
			List<CompanyForm> data = new ArrayList<CompanyForm>();
			List<Company> list = myService.findAll();
			for (Company po : list) {
				CompanyForm vo = new CompanyForm();
				BeanUtils.copyProperties(po, vo);
				data.add(vo);
			}
			dataList = list;
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String addEntity() {
		Company entity = new Company();
		BeanUtils.copyProperties(company, entity);
		Companytype companytype = new Companytype();
		companytype.setCompanyTypeId(company.getCompanyTypeId());
		entity.setCompanytype(companytype);
		entity.setActiveProject(0);
		entity.setArchiveProject(0);
		try {
			myService.save(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String editEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		Company entity = myService.findById(company.getCompanyId());
		entity.setCompanyName(company.getCompanyName());
		Companytype companytype = new Companytype();
		companytype.setCompanyTypeId(company.getCompanyTypeId());
		entity.setCompanytype(companytype);
		entity.setEmail(company.getEmail());
		entity.setPhone(company.getPhone());
		entity.setTelephone(company.getTelephone());
		entity.setAddress(company.getAddress());
		entity.setCity(company.getCity());
		entity.setCountry(company.getCountry());
		entity.setZipCode(company.getZipCode());
		entity.setUrl(company.getUrl());
		entity.setCompanyPerson(company.getCompanyPerson());
		entity.setDescribes(company.getDescribes());
		try {
			myService.update(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			Company entity = myService.findById(company.getCompanyId());
			if (myService.delete(entity)) {
				dataMap.put("valid", true);
			} else {
				dataMap.put("valid", false);
			}
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public void updateProjectNum() {
		List<Company> list = myService.findAll();
		for (Company c : list) {
			List<Projects> prolist = proService.findList("company.companyId",
					c.getCompanyId());
			int completeNum = 0;
			for (Projects p : prolist) {
				if (p.getProjectStatus().getProjectStatusId() == 6) {
					completeNum++;
				}
			}
			try {
				c.setActiveProject(prolist.size() - completeNum);
				c.setArchiveProject(completeNum);
				myService.update(c);
			} catch (Exception re) {

			}
		}
	}
}
